package com.example.labtest.webapi;


import com.example.labtest.core.PageUtils;
import com.example.labtest.domain.Student;
import com.example.labtest.domain.Student_info;
import com.example.labtest.helper.R;
import com.example.labtest.helper.RUtil;
import com.example.labtest.service.AdminService;
import com.example.labtest.service.StudentInfoService;
import com.example.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/studentInfo")
public class StudentInfoRestController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentInfoService studentInfoService;

    /**
     * 读取的url：/webapi/studentInfo/adlist
     *
     * @return
     */
    @GetMapping("/manageStuInfo")
    public List<Student_info> getAll() {
        List<Student_info> students = studentInfoService.findAll();
        return students;

    }

    /**
     * 分页,读取全部内容
     * 用如下两条语句传参数
     *
     * @return
     * @RequestParam(value="page",defaultValue = "0")Integer page,
     * @RequestParam(value="size",defaultValue = "10")Integer size
     */
    @GetMapping("/getbypage")
    public R<Student_info> getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "sname", defaultValue = "") String sname
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Student_info> student_infoPage;
        if (StringUtils.isEmpty(sname)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            student_infoPage = studentInfoService.findAll(pageable);
        } else {
            Student_info student_info = new Student_info();
            student_info.setSname(sname);

            Pageable pageable = (Pageable) PageRequest.of(0, 10, sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("sname", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student_info> example = Example.of(student_info, matcher);

            student_infoPage = studentInfoService.findAll(example, pageable);
        }
        PageUtils pageUtils = new PageUtils(student_infoPage.getContent(), student_infoPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
    @GetMapping("/getbySex")
    public PageUtils getBySex(@RequestParam(value = "page", defaultValue = "0") Integer page,

                              @RequestParam(value = "size", defaultValue = "10") Integer size,
                              @RequestParam(value = "sex", defaultValue = "") String sex
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Student_info> studentPage;
        if (StringUtils.isEmpty(sex)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentPage = studentInfoService.findAll(pageable);
        } else {
            Student_info student_info = new Student_info();
            student_info.setSex(sex);

            Pageable pageable = (Pageable) PageRequest.of(0, 10, sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("sex", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student_info> example = Example.of(student_info, matcher);

            studentPage = studentInfoService.findAll(example, pageable);
        }

        PageUtils pageUtils = new PageUtils(studentPage.getContent(), studentPage.getTotalElements());

        return pageUtils;

    }

    @GetMapping("/getbySclass")
    public PageUtils getBySclass(@RequestParam(value = "page", defaultValue = "0") Integer page,

                              @RequestParam(value = "size", defaultValue = "10") Integer size,
                              @RequestParam(value = "sclass", defaultValue = "") String sclass
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Student_info> studentPage;
        if (StringUtils.isEmpty(sclass)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentPage = studentInfoService.findAll(pageable);
        } else {
            Student_info student_info = new Student_info();
            student_info.setSclass(sclass);

            Pageable pageable = (Pageable) PageRequest.of(0, 10, sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("sclass", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student_info> example = Example.of(student_info, matcher);

            studentPage = studentInfoService.findAll(example, pageable);
        }

        PageUtils pageUtils = new PageUtils(studentPage.getContent(), studentPage.getTotalElements());

        return pageUtils;

    }


    @GetMapping("/getbySdept")
    public PageUtils getBySdept(@RequestParam(value = "page", defaultValue = "0") Integer page,

                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                 @RequestParam(value = "sdept", defaultValue = "") String sdept
    ) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Page<Student_info> studentPage;
        if (StringUtils.isEmpty(sdept)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentPage = studentInfoService.findAll(pageable);
        } else {
            Student_info student_info = new Student_info();
            student_info.setSdept(sdept);

            Pageable pageable = (Pageable) PageRequest.of(0, 10, sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("sdept", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student_info> example = Example.of(student_info, matcher);

            studentPage = studentInfoService.findAll(example, pageable);
        }

        PageUtils pageUtils = new PageUtils(studentPage.getContent(), studentPage.getTotalElements());

        return pageUtils;
    }

    /**
     * 新增学生的方法./webapi/studentInfo/insert(对应的url)
     *
     * @param student_info
     * @return
     */
    //新增是一个post操作，所以需要postMapping
    @PostMapping("/insert")
    public Student_info insert(Student_info student_info) {
        Student_info student_info1 = studentInfoService.insert(student_info);
        return student_info1;
    }

    /**
     * 读取的url：/webapi/admin/get/id
     *
     * @return
     */
    @GetMapping("/get/{id}")
    public Student_info get(@PathVariable Long id) {
        Student_info student_info = studentInfoService.getById(id);
        student_info.setPsw("");
        return student_info;

    }

    /**
     * 读取的url：/webapi/admin/findByName
     *
     * @return
     */

    @GetMapping("/findByName")
    public R<Student_info> findByName(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                 @RequestParam(value = "sname", defaultValue = "") String sname) {
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Student_info> student_infoPage;
        if (sname == null || sname.equals("")) {
            Pageable pageable = PageRequest.of(page, size, sort);
            student_infoPage = studentInfoService.findAll(pageable);
        } else {
            Pageable pageable = PageRequest.of(page, size, sort);
            student_infoPage = studentInfoService.findByNameLike("%" + sname + "%", pageable);
        }
        // List<Student> students = studentService.findByName(name);
        PageUtils pageUtils = new PageUtils(student_infoPage.getContent(), student_infoPage.getTotalElements());
        return RUtil.success(pageUtils);
    }


    /**
     * 更新学生的方法./webapi/admin/update(对应的url)
     *
     * @param student_info
     * @return
     */
    //新增是一个put操作，所以需要postMapping
    @PutMapping("/update")
    public Student_info update(Student_info student_info) {
        Student_info oldstudent = studentInfoService.getById(student_info.getId());
        if (StringUtils.isEmpty(student_info.getPsw())) {
            //没有新密码，那么就用旧密码
            student_info.setPsw(oldstudent.getPsw());
        }
        student_info = studentInfoService.update(student_info);
        return student_info;
    }

    /**
     * 删除学生的方法./webapi/admin/delete(对应的url)
     *
     * @param id
     */
    //修改成用路径的方式来删除
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {
        studentInfoService.delete(id);
    }
}
